﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenAuth.Repository.Domain;
using Infrastructure;
using OpenAuth.Repository.Interface;
using System.Data.SqlClient;
using System.Linq;

namespace OpenAuth.App
{
    /// <summary>
    /// 门诊预交金退费
    /// </summary>
    public class BMZXX009App : ApiBaseApp<BMZXX009>
    {
        public BMZXX009App(IApiRepository<BMZXX009> repository) : base(repository)
        {
        }
        /// <summary>
        /// 门诊预交金退费
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IEnumerable<BMZXX009Response> Refund(BMZXX009 model)
        {
            List<BMZXX009Response> list = new List<BMZXX009Response>();
            //没有yjxh导致退款的链接总是打开的报错
            if (model.yjxh==null)
            {
                list.Add(new BMZXX009Response { Code = 400, Message = "yjxh为空" });
            }
            else
            {
                List<SqlParameter> param = new List<SqlParameter>();
                param.Add(new SqlParameter("@Hzxm", model.name));
                param.Add(new SqlParameter("@Patid", model.patid));
                param.Add(new SqlParameter("@Yjxh", model.yjxh));
                param.Add(new SqlParameter("@Ptlsh", model.ptlsh));
                param.Add(new SqlParameter("@Czyh", "hf001"));
                string sql = " @hzxm=@Hzxm,@patid=@Patid,@yjxh=@Yjxh,@ptlsh=@Ptlsh,@czyh=@Czyh";
                var result = Put($"exec usp_web_webtrans_BMZXX009 {sql}", param);

                //返回成功结果
                if (result != null && result.Rows[0][0].ToString() == "T")
                {
                    list.Add(new BMZXX009Response {
                        tsjh = result.Rows[0][1].ToString(),
                        tyjxh = result.Rows[0][2].ToString(),
                        hisjylsh = result.Rows[0][3].ToString(),
                        tfje = result.Rows[0][4].ToString(),
                        zffs = result.Rows[0][5].ToString(),
                        zflsh = result.Rows[0][6].ToString(),
                        Code = 200
                    });
                }
                //返回错误结果
                else
                {
                    list.Add(new BMZXX009Response { Code = 400, Message = result.Rows[0][1].ToString() });
                }
            }
         

            return list.AsEnumerable<BMZXX009Response>();
           
        }
        /// <summary>
        /// 检查YY_PAYDETAILK表中是否有重复得退款记录
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="hisddh">his单号</param>
        /// <param name="jlzt">记录状态</param>
        /// <param name="paymoney">金额</param>
        /// <returns></returns>
        public bool CheckRefund(decimal patid,string hisddh,short jlzt, decimal paymoney)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Patid", patid));
            param.Add(new SqlParameter("@Hisddh", hisddh));
            param.Add(new SqlParameter("@Jlzt", jlzt));
            param.Add(new SqlParameter("@Paymoney", paymoney));
            string sql = "select * from YY_PAYDETAILK where patid=@Patid and hisddh=@Hisddh and jlzt=2 and paymoney=@Paymoney";
            var result = Put(sql, param);
            if (result.Rows.Count>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
         
    }
   
}
